package wxact

import (
	"myproject/wx_test/src/wxglob"
	"myproject/wx_test/src/wxmod"

	"encoding/json"
	"log"
	"time"

	"github.com/astaxie/beego/httplib"
)

type WXActMB struct{}

func (*WXActMB) MuBanMsg(msgRec *wxmod.XmlData) {
	baseData := make(map[string]interface{})
	baseData["value"] = "郁闷了"
	baseData["color"] = "#173177"

	keyword := make(map[string]interface{})
	keyword["keyword1"] = baseData
	keyword["keyword2"] = baseData
	keyword["keyword3"] = baseData
	keyword["first"] = baseData
	keyword["remark"] = baseData

	data := make(map[string]interface{})
	data["data"] = keyword
	data["touser"] = wxglob.Config.Section("wx").Key("zr").String()
	data["template_id"] = "eCjWXBLaf9FIT4AObEZM-pDEcom_DW0UB5XVRqXxNQQ"
	data["url"] = "https://wx.iyuedan.com/wap/share/FriendGet?uid=55937325&type=2&user_id=55702289&source=weixin"

	//json序列化
	dataByte, err := json.Marshal(data)
	if err != nil {
		log.Println(err)
		return
	}

	req := httplib.Post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + wxglob.AccessToken)
	result := req.Body(wxmod.BytesToStr(dataByte)).SetTimeout(50*time.Second, 50*time.Second)

	//转换成Json
	res := make(map[string]interface{})
	err = result.ToJSON(&res)
	if err != nil {
		log.Println(err)
		return
	}

	log.Println(res)
	log.Println(res["errcode"])

	return
}
